package com.zkx.blogweb.controller;

import com.zkx.blogweb.NumberUtils;
import com.zkx.blogweb.entity.Bolg;
import com.zkx.blogweb.entity.Message;
import com.zkx.blogweb.entity.User;
import com.zkx.blogweb.mapper.BolgMapper;
import com.zkx.blogweb.mapper.service.BlogServiceImp;
import com.zkx.blogweb.mapper.service.UserMapperImp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author zkx
 */
@Slf4j
@Controller
public class RegisterController {

    @Autowired
    UserMapperImp userMapperImp;

    @Autowired
    BlogServiceImp blogServiceImp;

    @ResponseBody
    @RequestMapping(value = "/register",method = {RequestMethod.POST,RequestMethod.GET})
    public String register(@RequestBody User user){
        //随机生成博客号
        int blogId = NumberUtils.random();
        boolean isExits = blogServiceImp.isExits(blogId);
        //如果存在,一致生成
        while(isExits){
            blogId = NumberUtils.random();
            isExits = blogServiceImp.isExits(blogId);
        }
        //不存在的话，就一起创建
        Bolg bolg = new Bolg();
        bolg.setBlogId(blogId);
        bolg.setBlogDescribe("你可以在这里面描述自己哦！！！");
        user.setUserBlog(blogId);

        //先创建自己的bolg
        blogServiceImp.createBlog(bolg);

        //按user_id不传入，主键自动生成。
        userMapperImp.insertSelective(user);
        System.out.println(user);
        return "成功";
    }
}
